#include "core/main.h"

OutputDataStream::OutputDataStream()
{
    
}

OutputDataStream::~OutputDataStream()
{
    
}

void OutputDataStream::SaveToFile(const string &filename)
{
    FILE *file = Utility::CheckedFOpen(filename.c_str(), "wb");
    
    const UINT MyLength = _data.size();

    //
    // This used to save the size of a file as the first four bytes,
    // but this is unncessary for uncompressed streams.
    //
    //Utility::CheckedFWrite(&MyLength, sizeof(UINT), 1, File);
    //

    Utility::CheckedFWrite(&(_data[0]), sizeof(BYTE), MyLength, file);
    fclose(file);
}

/*void OutputDataStream::SaveToCompressedFile(const string &Filename)
{
    Compression::CompressStreamToFile(_data, Filename);
}*/

void OutputDataStream::SaveToFileNoHeader(const string &filename)
{
    FILE *File = Utility::CheckedFOpen(filename.c_str(), "wb");
    Utility::CheckedFWrite(&(_data[0]), sizeof(BYTE), _data.size(), File);
    fclose(File);
}

void OutputDataStream::WriteData(const BYTE *Data, UINT BytesToWrite)
{
    if(BytesToWrite > 0)
    {
        UINT StartLength = _data.size();
        _data.resize(StartLength + BytesToWrite);
        memcpy(&(_data[0]) + StartLength, Data, BytesToWrite);
    }
}

OutputDataStream& operator << (OutputDataStream &S, const string &V)
{
    UINT Length = V.size();
    S << Length;
    S.WriteData((const BYTE *)V.c_str(), Length);
    return S;
}
